import smtplib
import os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication

from ui_framework_demo.common.getfiledir import REPORTDIR
from ui_framework_demo.common.get_email_datas import GetEmailDatas

class Opr_email(object):

    def __init__(self):
        """初始化文件路径与相关配置"""
        all_path = []
        for maindir, subdir, file_list in os.walk(REPORTDIR):
            pass
        for filename in file_list:
            all_path.append(os.path.join(REPORTDIR, filename))
        self.filename = all_path[0]
        self.host = GetEmailDatas.get_email()["host"]
        self.port = GetEmailDatas.get_email()["port"]
        self.user = GetEmailDatas.get_email()["user"]
        self.pwd = GetEmailDatas.get_email()["pwd"]
        self.from_addr = GetEmailDatas.get_email()["from_addr"]
        self.to_addr = GetEmailDatas.get_email()["to_addr"]

    def get_email_host_smtp(self):
        """连接stmp服务器"""
        self.smtp = smtplib.SMTP_SSL(host=self.host, port=self.port)
        self.smtp.login(user=self.user, password=self.pwd)

    def made_msg(self):
        """构建一封邮件"""
        self.msg = MIMEMultipart()
        with open(self.filename, 'rb') as f:
            content = f.read()
        # 创建文本内容
        text_msg = MIMEText(content, _subtype='html', _charset='utf8')
        # 添加到多组件的邮件中
        self.msg.attach(text_msg)
        # 创建邮件的附件
        report_file = MIMEApplication(content)
        report_file.add_header('Content-Disposition', 'attachment', filename=str.split(self.filename, '\\').pop())

        self.msg.attach(report_file)
        # 主题
        self.msg['subject'] = '自动化测试报告'
        # 发件人
        self.msg['From'] = self.from_addr
        # 收件人
        self.msg['To'] = self.to_addr

    def send_email(self):
        """发送邮件"""
        self.get_email_host_smtp()
        self.made_msg()
        self.smtp.send_message(self.msg, from_addr=self.from_addr, to_addrs=self.to_addr)